package com.thon.service.user.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thon.dao.user.LetterDao;
import com.thon.dao.user.UserDao;
import com.thon.dao.util.Page;
import com.thon.entity.user.Letter;
import com.thon.entity.user.LetterView;
import com.thon.entity.user.User;
import com.thon.service.user.LetterService;

@Service
@Transactional
public class LetterServiceImpl implements LetterService {

	@Autowired
	private LetterDao letterDao;
	@Autowired
	private UserDao userDao;

	@Override
	public void addLetter(Letter letter) {

		letterDao.save(letter);

	}

	@Override
	public void deleteLetter(Integer letterId) {

		letterDao.delete(letterId);
	}

	@Override
	public Page<LetterView> listOldLetters(Page<LetterView> page, Integer userId,
			Integer friendId,Date date) {

		Page<Letter> letterPage = new Page<Letter>();
		List<LetterView> letterViews = new ArrayList<LetterView>();

		letterPage.setPageNo(page.getPageNo());
		letterPage.setPageSize(page.getPageSize());
		letterDao.listOldLetters(letterPage, userId, friendId,date);
		for (Letter letter : letterPage.getResult()) {
			try {
				LetterView letterView = new LetterView();
				letterView.setId(letter.getId());
				letterView.setContent(letter.getContent());
				letterView.setIsRead(letter.getIsRead());
				letterView.setCreateDate(letter.getCreatedDate());

				User user = userDao.get(letter.getLetterFrom());
				letterView.setUserId(user.getId());
				letterView.setUserName(user.getName());
				//letterView.setUserRole(user.getRoles());
				letterView.setUserAttachment(user.getAvatarUrl());

				User friendUser = userDao.get(letter.getLetterTo());
				letterView.setFriendId(friendUser.getId());
				letterView.setFriendName(friendUser.getName());
				//letterView.setFriendRole(friendUser.getRoles());
				letterView.setFriendAttachmentId(friendUser.getAvatarUrl());

				letterViews.add(letterView);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		/*page.setPageNo(letterPage.getPageNo());
		page.setPageSize(letterPage.getPageSize());*/
		page.setResult(letterViews);
		page.setTotalCount(letterPage.getTotalCount());

		return page;
	}

	@Override
	public List<LetterView> listNewestLetters(Integer userId) {

		List<LetterView> letterViews = new ArrayList<LetterView>();
		List<Object> objects = letterDao.listFriends(userId);

		for (Object object : objects) {
			try {
				Object[] o = (Object[]) object;
				Integer friendId = Integer.valueOf(o[0].toString());
				Integer letterId = Integer.valueOf(o[1].toString());

				Letter letter = letterDao.get(letterId);

				LetterView letterView = new LetterView();
				letterView.setContent(letter.getContent());
				letterView.setCreateDate(letter.getCreatedDate());
				letterView.setIsRead(letter.getIsRead());

				User frinUser = userDao.get(friendId);
				letterView.setFriendId(frinUser.getId());
				letterView.setFriendName(frinUser.getName());
				letterView.setFriendAttachmentId(frinUser.getAvatarUrl());

				letterViews.add(letterView);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return letterViews;
	}

	@Override
	public Page<LetterView> listNewLetters(Page<LetterView> page,
			Integer userId, Integer friendId, Date date) {
		Page<Letter> letterPage = new Page<Letter>();
		List<LetterView> letterViews = new ArrayList<LetterView>();

		letterPage.setPageNo(page.getPageNo());
		letterPage.setPageSize(page.getPageSize());
		letterDao.listNewLetters(letterPage, userId, friendId,date);
		for (Letter letter : letterPage.getResult()) {
			try {
				LetterView letterView = new LetterView();
				letterView.setId(letter.getId());
				letterView.setContent(letter.getContent());
				letterView.setIsRead(letter.getIsRead());
				letterView.setCreateDate(letter.getCreatedDate());

				User user = userDao.get(letter.getLetterFrom());
				letterView.setUserId(user.getId());
				letterView.setUserName(user.getName());
				//letterView.setUserRole(user.getRoles());
				letterView.setUserAttachment(user.getAvatarUrl());

				User friendUser = userDao.get(letter.getLetterTo());
				letterView.setFriendId(friendUser.getId());
				letterView.setFriendName(friendUser.getName());
				//letterView.setFriendRole(friendUser.getRoles());
				letterView.setFriendAttachmentId(friendUser.getAvatarUrl());

				letterViews.add(letterView);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		/*page.setPageNo(letterPage.getPageNo());
		page.setPageSize(letterPage.getPageSize());*/
		page.setResult(letterViews);
		page.setTotalCount(letterPage.getTotalCount());

		return page;
	}

	@Override
	public Long countUnReadLetter(Integer isRead, Integer userId) {
		return letterDao.countLetter(isRead, userId);
	}

	@Override
	public void updateLetter(LetterView letterView) {

		Letter letter = letterDao.get(letterView.getId());
		letter.setIsRead(letterView.getIsRead());
		letterDao.update(letter);
		
	}

	@Override
	public Page<LetterView> listNewestLetters(Integer userId, Integer pageNo,
			Integer pageSize) {
		Page<LetterView> letterViews = new Page<LetterView>();
		letterViews.setPageSize(pageSize);
		List<LetterView> lViews=letterViews.getResult();
		
		Page<Letter> letters=new Page<Letter>();
		letters.setPageNo(letterViews.getPageNo());
		letters.setPageSize(letterViews.getPageSize());
		letters=letterDao.findNewLetters(letters, userId);

		for (Letter letter :letters.getResult()) {
			
				Integer friendId=letter.getFriendId();
				
				LetterView letterView = new LetterView();
				letterView.setContent(letter.getContent());
				letterView.setCreateDate(letter.getCreatedDate());
				letterView.setIsRead(letter.getIsRead());

				User frinUser = userDao.get(friendId);	
				if(frinUser!=null){
				letterView.setFriendId(frinUser.getId());
				letterView.setFriendName(frinUser.getName());
				//letterView.setFriendRole(frinUser.getRoles());
				letterView.setFriendAttachmentId(frinUser.getAvatarUrl());
				}
				
				lViews.add(letterView);
				
				letterViews.setResult(lViews);
			
		}
		letterViews.setPageNo(letters.getPageNo());
		letterViews.setAutoCount(letters.getAutoCount());
		letterViews.setTotalCount(letters.getTotalCount());
		return letterViews;
	}
}
